import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8899);
        while (true){
            Socket socket = serverSocket.accept();
            System.out.println("有一个客户端链接");
            Executors.newCachedThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream inputStream = socket.getInputStream();
                        byte[] bytes = new byte[1024];
//                        DataInputStream dataInputStream = new DataInputStream(inputStream);
//                        String s = dataInputStream.readUTF();//要与writeUTF同时是用否则出错
                        int len = inputStream.read(bytes);
                        String s = new String(bytes,0,len);//同时用字节流或者utf输入则不会出错
                        System.out.println(s+"  123132");
                    } catch (IOException e) {
                        try {
                            socket.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
